package com.hitqz.robot.driver.listener;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.google.common.util.concurrent.RateLimiter;
import com.hitqz.robot.api.business.event.TrackRemoteModeTriggerEvent;
import com.hitqz.robot.api.business.event.TrackStopTriggerEvent;
import com.hitqz.robot.api.business.event.TrackUltrasonicTriggerEvent;
import com.hitqz.robot.api.common.dto.robot.RobotGoTargetPosDto;
import com.hitqz.robot.api.common.dto.robot.RobotOnlineEventDto;
import com.hitqz.robot.api.common.entity.RobotMap;
import com.hitqz.robot.api.common.entity.RobotMapPos;
import com.hitqz.robot.common.core.util.R;
import com.hitqz.robot.driver.communication.NetworkServiceThreadPool;
import com.hitqz.robot.driver.util.WheelToolkit;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

@Component
@AllArgsConstructor
@Slf4j
public class WheelSelfListener {

    @EventListener
    public void onOnlineEvent(RobotOnlineEventDto event){
        NetworkServiceThreadPool.dealEvent(()->{
            //上线逻辑
            if (Objects.equals(event.getOnlineStatus(),1)){
                //切换地图...
                String currMapCode = WheelToolkit.getCurrMapCode();
                String dbMapCode = Optional.ofNullable(WheelToolkit.robotMap).map(RobotMap::getMapCode).orElse(currMapCode);
                if (!Objects.equals(dbMapCode,currMapCode)){
                    Integer type = WheelToolkit.robotMap.getType();
                    int needLocationMode = switch (type){
                        case 2->  2;
                        case 5 -> 1;
                        default -> 0; //2D
                    };
                    Integer currentLocationMode = WheelToolkit.getLocationMode();
                    if (!Objects.equals(needLocationMode,currentLocationMode)){
                        //关定位
                        WheelToolkit.setLocationEnable(0);
                        //切换定位模式
                        WheelToolkit.setLocationMode(needLocationMode);
                        log.info("[wheel]change location mode {}->{}",currentLocationMode,needLocationMode);
                    }
                    //切换地图
                    WheelToolkit.selMap(dbMapCode);
                    log.info("[wheel]change map {}->{}",dbMapCode,currMapCode);
                    if (Objects.equals(WheelToolkit.getLocationEnableStatus(),0)){
                        //开定位
                        WheelToolkit.setLocationEnable(1);
                    }
                }
            }else {
                //掉线逻辑


            }
        });
    }





}
